<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {% block title %}
        <title>index</title>
    {% endblock title %}
    {% load static %}
    <link rel="stylesheet" href="{% static 'bootstrap-3.4.1-dist/css/bootstrap.min.css' %}">
    <script src="{% static 'js/jquery-1.11.3.min.js' %}"></script>
    <script src="{% static 'js/csrf_token.js' %}"></script>
    <script src="{% static 'bootstrap-3.4.1-dist/js/bootstrap.min.js' %}"></script>
    <style>
        #user_avatar {
            width: 30px;
            height: 30px;
            border-radius: 50%;
        }

        #old_avatar, #new_avatar {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            margin-left: 20px;
        }
    </style>
    {% block css %}

    {% endblock css %}
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <a class="navbar-brand" href="{% url 'base:index' %}">选课系统</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                {% if request.path == '/' %}
                    <li class="active"><a href="{% url 'base:index' %}">首页 <span class="sr-only">(current)</span></a>
                    </li>
                {% else %}
                    <li><a href="{% url 'base:index' %}">首页</a></li>
                {% endif %}
                <li><a href="#">尚未开发</a></li>
            </ul>
            {% if request.user.is_authenticated %}
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="{% url 'base:index' %}">{{ request.user.username }}</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">
                            <img id="user_avatar" src="/media/{{ request.user.avatar }}" alt="用户头像">
                        </a>
                        <ul class="dropdown-menu">
                            <li><a href="#" data-toggle="modal" data-target="#edit_avatar" data-whatever="@mdo">
                                修改头像
                            </a></li>
                            <li><a href="#" data-toggle="modal" data-target="#edit_password"
                                   data-whatever="@mdo">修改密码</a></li>
                            <li><a href="#">期待更多...</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="{% url 'base:signout' %}">注销</a></li>
                        </ul>
                    </li>
                </ul>
            {% else %}
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="{% url 'base:signin' %}">登录</a></li>
                    <li><a href="{% url 'base:register' %}">注册</a></li>
                </ul>
            {% endif %}

        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-2">
            <div class="panel panel-default">
                <a href="{% url 'base:index' %}">
                    <div class="panel-body">首页</div>
                </a>
                <a href="{% url 'base:students' %}">
                    <div class="panel-footer">学生管理</div>
                </a>
                <a href="{% url 'base:classes' %}">
                    <div class="panel-footer">班级管理</div>
                </a>
                <a href="{% url 'base:course' %}">
                    <div class="panel-footer">课程管理</div>
                </a>
                <a href="{% url 'base:teacher' %}">
                    <div class="panel-footer">老师管理</div>
                </a>
            </div>
        </div>
        <div class="col-md-10">
            {% block content %}
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <div class="panel-title">首页</div>
                    </div>
                    <div class="panel-body">
                        <h4>欢迎进入选课系统</h4>
                        {% if request.user.last_login %}
                            <p>上一次登录：{{ request.user.last_login|date:'Y-m-d H:i:s' }}</p>
                        {% endif %}
                        <p>管理员：hkwJsxl@gmail.com</p>
                        <p>项目地址：<a href="https://github.com/HkwJsxl/CourseSelSystem" target="_blank">https://github.com/HkwJsxl/CourseSelSystem</a>
                        </p>
                    </div>
                </div>
            {% endblock content %}
        </div>
    </div>
</div>

<div class="modal fade" id="edit_avatar" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="exampleModalLabel">修改头像</h4>
            </div>
            <div class="modal-body">
                <form action="{% url 'base:edit_avatar' %}" method="post" enctype="multipart/form-data" novalidate>
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="old_avatar">原头像:</label>
                        <img src="/media/{{ request.user.avatar }}" alt="用户原头像" id="old_avatar">
                    </div>
                    <div class="form-group">
                        <label for="new_avatar_input">
                            新头像:<img src="/media/{{ request.user.avatar }}" alt="用户原头像" id="new_avatar">
                        </label>
                        <input type="file" id="new_avatar_input" name="new_avatar" class="hide">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="edit_password" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="exampleModalLabel">修改密码</h4>
            </div>
            <div class="modal-body">
                <form action="{% url 'base:edit_password' %}" method="post" enctype="multipart/form-data" novalidate>
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="old_password">原密码:</label>
                        <input type="password" name="old_password" id="old_password" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="new_password">新密码:</label>
                        <input type="password" name="new_password" id="new_password" class="form-control">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


<script>
    $('#new_avatar_input').change(function () {
        let myFileReaderObj = new FileReader();
        let fileObj = $(this)[0].files[0];
        myFileReaderObj.readAsDataURL(fileObj)
        myFileReaderObj.onload = function () {
            $('#new_avatar').attr('src', myFileReaderObj.result)
        }
    })
</script>
{% block js %}

{% endblock js %}
</body>
</html>
